﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.IO;
using BusinessLayer;
using log4net;
using System.Web.Hosting;
using System.Net;
using DTO;
using ModelLayer;
using BusinessLayer.Logic.ReportProducer;
using System.Collections.Generic;

namespace AssessmentTool.Web
{
    [ServiceContract(Namespace = "http://company.com/MyCompany.Services.MyProduct")]
    [SilverlightFaultBehavior]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class ReportService
    {
        private static readonly ILog log = LogManager.GetLogger(
                 System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        [OperationContract]
        public DownloadResponse createPDFRadar(DownloadRequestReportRadar request)
        {
            try
            {
                HostingEnvironment.ApplicationHost.GetVirtualPath();
                DownloadResponse dr = new DownloadResponse();
                dr.Stream = new PdfRadarProducer().produce(request.image, HostingEnvironment.MapPath("/htmlReport"), request.infoReport);
                return dr;
            }
            catch (Exception e)
            {
                log.Error("Errore nella chiamata", e);
                throw e;
            }
        }
        [OperationContract]
        public DownloadResponse createPDFCompetenze(DownloadRequestReportCompetenze request)
        {
            try
            {
                HostingEnvironment.ApplicationHost.GetVirtualPath();
                DownloadResponse dr = new DownloadResponse();
                dr.Stream = new PdfCompetenzeProducer().produce(request.utente, HostingEnvironment.MapPath("/htmlReport"));
                return dr;
            }
            catch (Exception e)
            {
                log.Error("Errore nella chiamata", e);
                throw e;
            }
        }
        [OperationContract]
        public DownloadResponse getProfiliUtentiExcel(DownloadProfiliExcel utenti)
        {
            try
            {
                DownloadResponse dr = new DownloadResponse();
                dr.Stream=new ExcelUtentiProfili().getExcelUtentiProfili(utenti.utenti);
                return dr;
            }
            catch (Exception e)
            {
                log.Error("Errore nella chiamata", e);
                throw e;
            }
        }
        [OperationContract]
        public DownloadResponse importUtenti(UploadImport utenti)
        {
            try
            {
                DownloadResponse dr = new DownloadResponse();
                if (utenti.Stream == null)
                    dr.Stream = new ExcelImportUtenti().getTemplate();
                else
                    dr.Stream = new ExcelImportUtenti().import(utenti.Stream,utenti.utente);
                return dr;
            }
            catch (Exception e)
            {
                log.Error("Errore nella chiamata", e);
                throw e;
            }
        }
    }
    

    [MessageContract]
    public class DownloadResponse
    {
        [MessageBodyMember]
        public byte[] Stream;

    }
    [MessageContract]
    public class UploadImport
    {
        [MessageBodyMember]
        public byte[] Stream;
        [MessageBodyMember]
        public Utente utente;
    }
    [MessageContract]
    public class DownloadProfiliExcel
    {
        [MessageBodyMember]
        public List<DTOUtente> utenti;

    }
    
    [MessageContract]
    public class DownloadRequestReportRadar
    {
        [MessageBodyMember]
        public ReportRadarInfoDTO infoReport;

        [MessageBodyMember]
        public byte[] image;
    }
    [MessageContract]
    public class DownloadRequestReportCompetenze
    {
        [MessageBodyMember]
        public Utente utente;
    }

}
